/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.Vector;
import searchers.Searcher;

/**
 *
 * @author zimi
 */
public class Cookbook {

    private Diet userDiet;
    // @TODO przerobić na hashtable?
//    private Vector /*<Recipe>*/ recipes = new Vector();
    private SearchList recipes = new SearchList();
    private Vector /*<Category>*/ possibleCategories = new Vector();
    private Vector /*<Measure>*/ possibleMeasures = new Vector();
    private Vector /*<Product>*/ possibleProducts = new Vector();
    //@Transient
    private Vector /*<Searcher>*/ searchers = new Vector();

    public Diet getUserDiet() {
        return userDiet;
    }

    public void setUserDiet(Diet userDiet) {
        this.userDiet = userDiet;
    }

    public void addSearcher(Searcher searcher) {
        searchers.addElement(searcher);
    }

    private void clearSearchers() {
        searchers = new Vector();
    }

 
//    public Vector/*<Recipe>*/ list(boolean all) {
//        if(all) {
//            clearSearchers();
//            return recipes;
//        } else {
//            Vector resultSet = new Vector();
//            // chyba nie ma takiej pętli
//            for(int i = 0; i < recipes.size(); i++) {
//                Recipe recipe = (Recipe)recipes.elementAt(i);
//                if(verifyRecipe(recipe)) {
//                    resultSet.addElement(recipe);
//                }
//            }
//            clearSearchers();
//            return resultSet;
//        }
//    }
    public SearchList/*<Recipe>*/ list(boolean all) {
        if (all) {
            clearSearchers();
            return recipes;
        } else {
            SearchList resultSet = new SearchList();
            // chyba nie ma takiej pętli
            for (int i = 0; i < recipes.size(); i++) {
                Recipe recipe = (Recipe) recipes.elementAt(i);
                if (verifyRecipe(recipe)) {
                    resultSet.addElement(recipe);
                }
            }
            clearSearchers();
            return resultSet;
        }
    }

    private boolean verifyRecipe(Recipe recipe) {
        for (int i = 0; i < searchers.size(); i++) {
            Searcher searcher = (Searcher) searchers.elementAt(i);
            if (searcher == null) {
                continue;
            }
            if (!searcher.isHit(recipe)) {
                return false;
            }
        }
        return true;
    }

    //@TODO dodawanie do list kategorii które się na niej jeszcze nie znajdują
    public boolean addPossibleCategory(Category category) {
        if (category == null) {
            return false;
        }
        if (!possibleCategories.contains(category)) {
            possibleCategories.addElement(category);
            return true;
        }
        return false;
    }

    public boolean removePossibleCategory(Category category) {
        return possibleCategories.removeElement(category);
    }

    public Vector/*<Category>*/ listPossibleCategories() {
        return possibleCategories;
    }

    public boolean addPossibleProduct(Product product) {
        try{
            if (!possibleProducts.contains(product)) {
                possibleProducts.addElement(product);
                return true;
            }
        }catch(NullPointerException e){
            //System.out.println("null");
            //TODO
        }
        return false;
    }

    public boolean removePossibleProduct(Product ingredient) {
        return possibleProducts.removeElement(ingredient);
    }

    public Vector/*<Product>*/ listPossibleProducts() {
        return possibleProducts;
    }

    public boolean addPossibleMeasure(Measure measure) {
        if (!possibleMeasures.contains(measure)) {
            possibleMeasures.addElement(measure);
            return true;
        }
        return false;
    }

    public boolean removePossibleMeasure(Measure measure) {
        return possibleMeasures.removeElement(measure);
    }

    public Vector/*<Measure>*/ listPossibleMeasures() {
        return possibleMeasures;
    }

    public boolean addRecipe(Recipe recipe) {
        if(recipe == null){
            return false;
        }

        if (!recipes.contains(recipe)) {
            recipes.addElement(recipe);
            return true;
        }
        return false;
    }

    public boolean removeRecipe(Recipe recipe) {
        return recipes.removeElement(recipe);
    }

    public SearchList/*<Recipe>*/ listRecipes() {
        return recipes;
    }

    public Object listRecipesAsAVector() {
        SearchList searchList = listRecipes();
        Vector vector = new Vector();
        for(int i=0;i<searchList.size();i++){
            vector.addElement(searchList.elementAt(i));
        }
        return vector;
    }
}
